package ability

import (
	"sjy/customcmd/src/constants"
	"sjy/customcmd/src/entity"
	"sjy/customcmd/src/event"
)

var MasterCmdInstance = FuncManager.Register(
	&MasterCmdClass{
		FuncObjBase: &FuncObjBase{
			ClassName: constants.MasterCmd.Code,
		},
	},
)

var MasterCmdDefaultConfig = &entity.MasterCmdConfig{
	Task: "help",
}

type MasterCmdClass struct {
	*FuncObjBase
}

func (r *MasterCmdClass) Execute(obj *FuncEntity) {
	MasterCmdExcute(obj)
}

func MasterCmdExcute(obj *FuncEntity) {
	config := getMasterCmdConfig()
	if config.Task == constants.MasterCmd.Code {
		HelpInstanse.Execute(obj)
		return
	}
	funcClass := FuncManager.FindFunc(config.Task)
	if funcClass == nil {
		HelpInstanse.Execute(obj)
		return
	}
	funcClass.Execute(obj)
}

func getMasterCmdConfig() *entity.MasterCmdConfig {
	result := event.GetConfig(func(obj *entity.MainConfig) entity.FieldInterface {
		return obj.Master
	})
	// 类型断言
	config, ok := result.(*entity.MasterCmdConfig)
	if ok && config != nil && !config.AllEmpty() {
		return config
	} else {
		return MasterCmdDefaultConfig
	}
}
